<template>
	<view class="bg">
		<view class="center">
			<view class="center_list">
				<view class="center_li" v-for="(item,index) in menu" :key="index" @click="toPages(item.url, item.id)" >
					<text>{{item.text}}</text>
					<u-icon name="arrow-right" color="#999" size="20"></u-icon>
				</view>
				
				
			</view>
		</view>
		<view class="center">
			<view class="center_list">
				
				<view class="center_li" @click="removeAccount()">
					<text>账号注销</text>
					<u-icon name="arrow-right" color="#999" size="20"></u-icon>
				</view>
				<view class="center_li" @click="clearCacheEvent()" v-if="is_app">
					<text>清除缓存</text>
					<u-icon name="arrow-right" color="#999" size="20"></u-icon>
				</view>
				<view class="center_li" @click="outLogin()">
					<text>安全退出</text>
					<u-icon name="arrow-right" color="#999" size="20"></u-icon>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
		export default {
			data() {
				return {
					menu:[
						//{id: '0', text:'我的资产',icon:'rmb-circle',url:'/subone/user/wallet'},
						//{id: '1', text:'地址管理',icon:'map',url:'/subone/address/address?user=1'},
						//{id: '2', text:'浏览记录',icon:'eye',url:'/subone/user/browse'},
						{id: '3', text: '修改密码', icon: 'edit-pen', url: '/subone/user/changeAccount'},
						{id: '4', text: '账号找回', icon: 'account',url:'/subone/user/account'},
						//{id: '5', text: '隐私设置', icon: 'file-text', url: '/subone/user/set'},
						
						// {id: '6', text:'专属客服',icon:'server-fill',url:'../message/chat?userId=34e1b600b1fd579f47433b88e8d85291&name=客服'},
						{id: '7', text:'个人资料',icon:'question-circle',url:'/subone/user/userInfo'},
						{id: '8', text:'用户协议',icon:'pushpin',url:'/subone/public/agreement'},
						{id: '9', text:'隐私政策',icon:'error-circle',url:'/subone/public/privacy'},
						
						// {id: '10', text:'邀请好友',icon:'share',url:'/subone/user/invite'},
						{id: '11', text:'意见反馈',icon:'file-text',url:'/subone/user/feedback'},
						//{id: '12', text: '联系客服', icon: 'zhuanfa', url: '/subone/chat/chat?js_user_id=439&nickname=官方客服小熊'},
					],
					userInfo:'',
					is_app:false
				}
			},
			onShow() {
				this.userInfo = uni.getStorageSync('userInfo'); 
				
				
			},
			onLoad() {
				//#ifdef APP-PLUS
				this.is_app = true;
				//#endif
				this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight + 'px';
			},
			methods: {
				send_distribute(type){
					var that = this;
					if(type == 1){
						var title = '确认申请为团长？'; 
						var content = '成为团长后可以做任务获取佣金'; 
					}else{
						var title = '确认申请为商家？'; 
						var content = '成为商家后可分发任务推广'; 
					}
					if(!that.userInfo.openid){
						//授权拿openid
						uni.login({
							success: function (res) {
								console.log(res);
								uni.request({
									url:'https://oa.yuxiang123.cn/api/index/getOpenid',
									method:'POST',
									header:{"content-type":"application/x-www-form-urlencoded"},
									data:{'code':res.code,token: uni.getStorageSync('token'),'is_shop':1},
									success: (res) => {
										console.log(res);
										uni.showModal({
											title: title, // 标题
											content: content, // 内容(灰色字体)
											showCancel: true, // 显示取消按钮
											cancelText: '取消', // 取消按钮文字
											cancelColor: '#000000', // 取消按钮颜色
											confirmText: '确定', // 确定按钮文字
											confirmColor: '#007aff', // 确定按钮颜色
											success: (res1) => {
												if (res1.confirm) {
													if(type == 1){
														uni.request({
															url: 'https://oa.yuxiang123.cn/api/distribute_task/send_distribute',
															header: {
																"content-type": "application/x-www-form-urlencoded"
															},
															method: 'POST',
															data: {
																token: uni.getStorageSync('token'),
															},
															success: (res2) => {
																that.$Toast(res2.data.msg);
																if(res2.data.code == 1){
																	that.userInfo.is_task = 1;
																	uni.setStorageSync('userInfo',that.userInfo);
																}
															}
														})
													}else{
														uni.navigateTo({
															url:'/merchant/shop/add_shop'
														})
													}
												} else {
													console.log('用户点击取消');
												}
											}
										})
									}
								})	
							}					
						});
					}else{
						uni.showModal({
							title: title, // 标题
							content: content, // 内容(灰色字体)
							showCancel: true, // 显示取消按钮
							cancelText: '取消', // 取消按钮文字
							cancelColor: '#000000', // 取消按钮颜色
							confirmText: '确定', // 确定按钮文字
							confirmColor: '#007aff', // 确定按钮颜色
							success: (res) => {
								if (res.confirm) {
									if(type == 1){
										uni.request({
											url: 'https://oa.yuxiang123.cn/api/distribute_task/send_distribute',
											header: {
												"content-type": "application/x-www-form-urlencoded"
											},
											method: 'POST',
											data: {
												token: uni.getStorageSync('token'),
											},
											success: (res) => {
												that.$Toast(res.data.msg);
												if(res.data.code == 1){
													that.userInfo.is_task = 1;
													uni.setStorageSync('userInfo',that.userInfo);
												}
											}
										})
									}else{
										uni.navigateTo({
											url:'/merchant/shop/add_shop'
										})
									}
								} else {
									console.log('用户点击取消');
								}
							}
						})
				
					}

				},
				//注销账号
				removeAccount:function(){
					uni.navigateTo({
						url: "/subone/user/removeAccount"
					})
				},
				toPages:function(url, id){
					this.popupShow = false;
					if(id == '4' && id){
						uni.showModal({
							title: '提示',
							content: '您随时可以通过您注册的手机号收到的验证码登录到您的账户。如果您的账号不能正常登录，或者由于手机号变更不能正常登录，您可以通过苏拉APP【我】-【≡】-【意见反馈】，详细说明情况，我们会尽快为您处理。',
							showCancel: false,
							success: () => {}
						})
					}else{
						this.popupShow = false;
						uni.navigateTo({
							url:url
						})
					}
					
				},
				//退出登录
				outLogin:function(){
					this.popupShow = false
					uni.showModal({
					    title: '提示',
					    content: '确定要退出当前账号吗？',
					    success: function (res) {
					        if (res.confirm) {
								uni.clearStorageSync();
								uni.removeTabBarBadge({
									index:3
								});
								uni.removeTabBarBadge({
									index:4
								});
								uni.reLaunch({
									url:'/pages/shop/shopClass'
								})
					        } else if (res.cancel) {
					            console.log('用户点击取消');
					        }
					    }
					});
				},
				formatSize() {
					let that = this;
					plus.cache.calculate(function(size) {
						let sizeCache = parseInt(size);
						if (sizeCache == 0) {
							that.fileSizeString = "0B";
						} else if (sizeCache < 1024) {
							that.fileSizeString = sizeCache + "B";
						} else if (sizeCache < 1048576) {
							that.fileSizeString = (sizeCache / 1024).toFixed(2) + "KB";
						} else if (sizeCache < 1073741824) {
							that.fileSizeString = (sizeCache / 1048576).toFixed(2) + "MB";
						} else {
							that.fileSizeString = (sizeCache / 1073741824).toFixed(2) + "GB";
						}
					});
				},
				clearCacheEvent() {
					console.log('清除缓存')
					let that = this;
					let os = plus.os.name;
					if (os == 'Android') {
						let main = plus.android.runtimeMainActivity();
						let sdRoot = main.getCacheDir();
						let files = plus.android.invoke(sdRoot, "listFiles");
						let len = files.length;
						for (let i = 0; i < len; i++) {
							let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径  
							plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
								if (entry.isDirectory) {
									entry.removeRecursively(function(entry) { //递归删除其下的所有文件及子目录  
										uni.showToast({
											title: '缓存清理完成',
											duration: 2000
										});
										that.formatSize(); // 重新计算缓存  
									}, function(e) {
										//console.log(e.message)
									});
								} else {
									entry.remove();
								}
							}, function(e) {
								console.log('文件路径读取失败')
							});
						}
					} else { // ios  
						plus.cache.clear(function() {
							uni.showToast({
								title: '缓存清理完成',
								duration: 2000
							});
							that.formatSize();
						});
					}
				},
				
			},
			created() {
				// #ifdef APP-PLUS
					this.formatSize()
				// #endif
			}
		}
</script>

<style>
.bg{background-color: #f4f5f7;}
.center{background-color: #fff;padding:0 30rpx;margin-bottom: 20rpx;}
.center .center_title{height:80rpx;line-height: 80rpx;font-size: 36rpx;}
.center .center_list{width:100%;}
.center .center_list .center_li{width:100%; height:100rpx;line-height:100rpx;font-weight:600;color:#333; font-size: 32rpx;border-bottom: 1rpx solid #ddd;display: flex;flex-direction: row;justify-content: space-between;}
.nodata{width:100%;height: 100rpx;;}
</style>